<?php
namespace PhalconTutorial\Controllers;

use PhalconTutorial\Forms;
use PhalconTutorial\Forms\SignUpForm;
use PhalconTutorial\Models\Users;

class SessionController extends ControllerBase
{
    public function initialize()
    {
        $this->view->setTemplateBefore('skins/backend/default/template');
    }

    public function indexAction()
    {

    }

    public function signupAction()
    {
        $form = new SignUpForm();

        if($this->request->isPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                $user = new Users();
                $user->assign(array(
                    'name' => $this->request->getPost('name' , 'striptags'),
                    'email' => $this->request->getPost('email'),
                    'password' => $this->security->hash($this->request->getPost('password')),
                    'profilesId' => 2
                ));
                
                if($user->save())
                {
                    return $this->dispatcher->forward(array(
                        'controller' => 'index',
                        'action' => 'index'
                    ));
                }
                $this->flash->error($user->getMessages());
            }
        }

        $this->view->form = $form;
    }

}

